package com.lmx.healthcode.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.lmx.healthcode.model.bean.Student;

/**
 * @author Leung
 * @date 2020/9/12 10:58
 */
public class SharedPreUtil {

    private static final String CONFIG_FILE_NAME = "config";

    public static final String KEY_FIRST_COMMIT = "first_commit";
    public static final String KEY_STUDENT_ID = "student_id";
    public static final String KEY_STUDENT_NAME = "student_name";
    public static final String KEY_COLLEGE = "college";
    public static final String KEY_STUDENT_CLASS = "student_class";
    public static final String KEY_COUNSELOR = "counselor";
    public static final String KEY_COUNSELOR_PHONE = "counselor_phone";
    public static final String KEY_DORMITORY = "dormitory";
    public static final String KEY_TYPE = "type";
    public static final String KEY_OUT_SCHOOL_TIME = "out_school_time";
    public static final String KEY_OUT_SCHOOL_DOOR = "out_school_door";
    public static final String KEY_START_TIME = "start_time";
    public static final String KEY_END_TIME = "end_time";
    public static final String KEY_OBJECT_ID = "object_id";

    private SharedPreUtil() {
    }

    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(CONFIG_FILE_NAME,
                Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    public static void saveString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(CONFIG_FILE_NAME, Context
                .MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(
                CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }

    public static void saveBoolean(Context context, String key, Boolean value) {
        SharedPreferences sp = context.getSharedPreferences(
                CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(CONFIG_FILE_NAME, Context
                .MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(
                CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static Long getLong(Context context, String key, Long defValue) {
        SharedPreferences sp = context.getSharedPreferences(
                CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    public static void saveLong(Context context, String key, Long value) {
        SharedPreferences sp = context.getSharedPreferences(
                CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    /**
     * 保存学生信息到本地
     *
     * @param context 上下文
     * @param student 学生信息
     */
    public static void saveLocal(Context context, Student student) {
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_STUDENT_ID, student.getStudentId() + "");
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_STUDENT_NAME, student.getName());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_COLLEGE, student.getCollege());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_STUDENT_CLASS, student.getStudentClass());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_COUNSELOR, student.getCounselor());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_COUNSELOR_PHONE, student.getCounselorPhone() + "");
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_DORMITORY, student.getDormitory());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_TYPE, student.getType());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_OUT_SCHOOL_TIME, student.getOutSchoolTime());
        SharedPreUtil.saveString(context, SharedPreUtil.KEY_OUT_SCHOOL_DOOR, student.getOutSchoolDoor());
    }

    /**
     * 获取本地学生信息
     *
     * @param context 上下文
     * @return 学生信息
     */
    public static Student getLocal(Context context) {
        return new Student(
                // 学号 (返回0是用来初始化显示)
                Integer.parseInt(SharedPreUtil.getString(context, SharedPreUtil.KEY_STUDENT_ID, "0")),
                // 姓名
                SharedPreUtil.getString(context, SharedPreUtil.KEY_STUDENT_NAME, ""),
                // 学院 (返回0是用来初始化显示)
                SharedPreUtil.getString(context, SharedPreUtil.KEY_COLLEGE, "0"),
                // 班级
                SharedPreUtil.getString(context, SharedPreUtil.KEY_STUDENT_CLASS, ""),
                // 辅导员
                SharedPreUtil.getString(context, SharedPreUtil.KEY_COUNSELOR, ""),
                // 辅导员电话
                Long.parseLong(SharedPreUtil.getString(context, SharedPreUtil.KEY_COUNSELOR_PHONE, "0")),
                // 宿舍
                SharedPreUtil.getString(context, SharedPreUtil.KEY_DORMITORY, ""),
                // 住宿/走读 (返回0是用来初始化显示)
                SharedPreUtil.getString(context, SharedPreUtil.KEY_TYPE, "0"),
                // 出校时间
                SharedPreUtil.getString(context, SharedPreUtil.KEY_OUT_SCHOOL_TIME, ""),
                // 出校大门 (返回0是用来初始化显示)
                SharedPreUtil.getString(context, SharedPreUtil.KEY_OUT_SCHOOL_DOOR, "0")
        );
    }

}
